<template>
  <div>
    <bread></bread>
    <!-- 面包屑 -->
    <el-card>
      <el-button type="primary">添加角色</el-button>
    </el-card>
    <!-- 展开行 -->
    <el-table :data="rolesData" style="width: 100%">
      <!-- 展开行 -->
      <el-table-column type="expand">
        <template slot-scope="scope">
          <!-- 循环一级权限 -->
          <el-row
            :class="['bdbottom',i1==0?'bdtop':'','row-padding']"
            v-for="(item,i1) in scope.row.children"
            :key="item.id"
          >
            <el-col :span="5" class="h-center">
              <el-tag
                closable
                effect="dark"
                @close="removeRightById(scope.row,item.id)"
              >{{item.authName}}</el-tag>
              <i class="el-icon-caret-right"></i>
            </el-col>
            <el-col :span="19">
              <!-- 循环 二  三级权限 -->
              <!-- 二级权限 -->
              <el-row v-for="item2 in item.children" :key="item2.id">
                <el-col :span="6">
                  <el-tag
                    closable
                    type="success"
                    effect="dark"
                    @close="removeRightById(scope.row,item2.id)"
                  >{{item2.authName}}</el-tag>
                </el-col>
                <!-- 三级权限 -->
                <el-col :span="18">
                  <el-tag
                    closable
                    type="warning"
                    effect="dark"
                    v-for="item3 in item2.children"
                    :key="item3.id"
                    @close="removeRightById(scope.row,item3.id)"
                  >{{item3.authName}}</el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="#" type="index"></el-table-column>
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 修改 -->
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <!-- 删除 -->
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>

          <!-- 分配权限 -->
          <el-button
            type="warning"
            icon="el-icon-setting"
            size="mini"
            @click="showDialogRight(scope.row)"
          >分配权限</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" :visible.sync="dialogFormVisible">
      <!-- 树形控件
      data	展示数据	array
      show-checkbox	节点是否可被选择(显示复选框)	boolean
      node-key	设置选中节点对应的值	String
      default-checked-keys	默认选中的key的数组 key 的数组	array
      default-expand-all	是否默认展开所有节点	boolean
      props	配置选项，具体看下表	object
       -->
      <el-tree
        ref="treeRef"
        :data="rightslist"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defkeys"
        :props="defaultProps"
      ></el-tree>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import rolesApi from "@/api/roles.js"
import bread from "@/components/bread";
export default {
  components: {
    bread,
  },
  data() {
    return {
      rolesData: [],
      dialogFormVisible: false,
      rightslist:[],   //权限列表
      defaultProps: {   //树形控件的属性绑定对象
          children: 'children',
          label: 'authName'
        },
      roleId:"",  //角色id
      defkeys:[]  //设置树形控件中默认选中的内容
    };
  },
  created() {
    this.getroles();
  },
  methods: {

    getroles() {
      //获取角色列表数据
      rolesApi.getData().then((res) => {
        console.log(res.data, "角色内容");
        this.rolesData = res.data;
      });
    },
    showDialogRight(role) {  //点击分配权限执行的代码
    // 将角色id的保存下来  供保存权限列表使用
     this.roleId= role.id

      // 1：从服务端请求权限列表内容
      rolesApi.allocation()
      .then(res=>{
        // console.log(res.data,"树形")
        if(res.meta.status==200){
          this.rightslist=res.data
          // 把角色下的三级权限添加到defkeys数组中
          this.getdefkeys(role,this.defkeys)
        }
      })
      this.dialogFormVisible = true;
    },
    getdefkeys(node,arr){
        if(!node.children){
          return arr.push(node.id)
        }
        // 递归方式  
        node.children.forEach(item=>{
          this.getdefkeys(item,arr)
        })
    },
    allotRights(){
      console.log(this.$refs.treeRef.getCheckedKeys(),"全选中节点的key组成的数组")  //[101,105,106]
      console.log(this.$refs.treeRef.getHalfCheckedKeys(),"半选中节点的key组成的数组") 
  
     const keys=[
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
     ]
     
    const  idStr=keys.join(',')

    rolesApi.power(this.roleId,idStr)
    .then(res=>{
       console.log(res.meta.status)
        if(res.meta.status!=200){
            return this.$message.error("分配权限失败")
        }
        this.$message.success("分配权限成功")
         this.getroles()  //更新角色列表内容
// 关闭分配权限对话框
        this.dialogFormVisible=false
    })

  
  },
    removeRightById(role, rightId) {
      //权限删除功能
      this.$confirm("此操作将永久删除该权限, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 成功
          rolesApi.remove(role.id,rightId)
            .then((res) => {
              //    console.log(res,"shanc")
              if (res.meta.status == 200) {
                role.children = res.data;
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                // this.getroles()
              }
            });
        })
        .catch(() => {
          // 取消
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      //  尾部
    },
  },
};
</script>
<style>
.bdbottom {
  border-bottom: 1px solid #efefef;
}
.bdtop {
  border-top: 1px solid #efefef;
}
.bdleft {
  border-left: 1px solid #efefef;
}
.row-padding {
  padding: 15px;
}
.el-tag {
  margin: 10px;
}
.el-tag .el-icon-close {
  color: #fff;
}
</style>